#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "xml.h"

/* input:		the type of xml type. */
/* output:  	the string value of xml */
int xml_getstr(struct xml_element* e, const char* xpath, char *pstr, int index)
{
    char *p = NULL;
    struct xml_element** i;
    void** query;
    int n = 0;

    
	query = xml_get(e, xpath);
	if ( NULL != query && pstr != NULL )
	{
        i = (struct xml_element**)query;
        for (  ; i && *i; ++i ) 
        {
            if ( n == index )
            {
                break;
            }
            n++;
        }

        if ( *i != NULL )
        {
            if ( (( (struct xml_element*)i[0])->value ) == NULL )
            {
                p = strcpy(pstr, " ");
            }
            else
            {
    	        p = strcpy(pstr, ((struct xml_element*)i[0])->value);
    	    }
    	    
    	    if ( p == NULL )
    	    {
    	        free_xml_list( query );
    	        return -1;
    	    }
    	    free_xml_list( query );
    	    return 0;
	    }
	    else
	    {
	        pstr = NULL;
    	    free_xml_list( query );
    	    return -1;
	    }
	}
    
	return -1;
}

/* input:		the type of xml type. */
/* output:  	the number value of xml */
int xml_getint(struct xml_element* e, const char* xpath, int *dst, int index )
{	
    //void **i;
    struct xml_element** i;
    void** query;
    int n = 0;
    
	query = xml_get(e, xpath);
	if (NULL != query )
	{
        i = (struct xml_element**)query;
        for ( ; i && *i; ++i ) 
        {
            if ( n == index )
            {
                break;
            }
            n++;
        }
        
        if ( *i != NULL )
        {
            if ( (( (struct xml_element*)i[0] )->value ) == NULL )
            {
                free_xml_list( query );
                return -1;
            }
            else
            {
                *dst = atoi(((struct xml_element*)i[0])->value);
                free_xml_list( query );
                return 0;
            }
        }
        else
        {
            free_xml_list( query );
            return -1;
        }
	}
	else
	{
		return -1;
	}

	return -1;
}

int xml_getnodenum(struct xml_element* e, const char* xpath )
{	
    //void **i;
    struct xml_element** i;
    void** query;
    int n = 0;
    
	query = xml_get(e, xpath);
	if (NULL != query)
	{
        i = (struct xml_element**)query;
        for ( ; i && *i; ++i ) 
        {
            n++;
        }
        
        free_xml_list( query );
		return n;
	}
	else
	{
	    //printf("query is NULL\n");
		return -1;
	}

	return -1;
}

#if 0
int main (int argc, char *argv[])
{
	int fd, rn, val;	
    char buf[1024];
    
    struct xml_element *e = NULL;
	/*open xml file*/
    fd = open("./sample.xml", O_RDWR);
    if ( fd < 0 )
    {
        return -1;
    }

	/*read xml file*/
    memset(buf, 0x0, sizeof(buf));
    rn = read(fd, buf, sizeof(buf));
    if ( rn < 0 )
    {
        return -1;
    }
	
	/*get the root of the xml*/
    e = load_xml(buf, rn);
    if ( e != NULL )
    {
        #if 1
		/*read data*/
		xml_getstr(e, "/note/to",buf);
		printf("pto=%s\n",buf);
		
		xml_getstr(e, "/note/from",buf);
		printf("from=%s\n",buf);
		
		xml_getint(e, "/note/heading",&val);
		printf("heading=%d\n",val);
		
		xml_getint(e, "/note/body",&val);
		printf("body=%d\n",val);
		#endif
        free_xml(e);
    }
	
    return 0;
	
}
#endif


